#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#  Copyright (c) Huoty - All rights reserved
#
#      Author: Huoty <sudohuoty@gmail.com>
#  CreateTime: 2016-10-15 12:52:06
# *************************************************************

"""微信公众平台接口"""

import os
import hashlib


class Wechat(object):
    '''微信公众号接口'''

    def __init__(self, appid, appsecret, token=None, encoding_aeskey=None, encrypt_mode=None):
        self.appid = appid
        self.appsecret = appsecret
        self.token = token
        self.encoding_aeskey = encoding_aeskey
        self.encrypt_mode = encrypt_mode

        # try:
        #     self._encodingAesKey = b64decode(
        #         self.encodingAesKey.encode('utf8') + b'=')
        # except:
        #     raise ParameterError('Wrong AES Key format')

    def check_signature(self, signature, timestamp, nonce):
        """接入验证"""
        s = ''.join(sorted([self.token, timestamp, nonce]))
        return sha1(s) == signature

    def parse_data(self, data):
        pass


def sha1(data):
    if isinstance(data, str):
        data = data.encode("utf-8")
    return hashlib.sha1(data).hexdigest()


# 公众号: 大米与宝贝
dami_baby_wechat = Wechat(
    appid = os.environ["APPID"],
    appsecret = os.environ["APPSECRET"],
    token = os.environ["WXPNO_TOKEN"],
    encoding_aeskey = os.environ["WXPNO_AESKEY"],
    encrypt_mode = None,
)


# Script starts from here
